#include "graphicx.h"

SDL_Surface *load_image(const char * a_filename) {
	SDL_Surface* loadedImage = NULL;
	SDL_Surface* optimizedImage = NULL;
	loadedImage = SDL_LoadBMP( a_filename );
	if( loadedImage != NULL )
	{ 
		optimizedImage = SDL_DisplayFormat( loadedImage );
		SDL_FreeSurface( loadedImage );
	}
	return optimizedImage;
}

void setTransparentColor(SDL_Surface * a_surface, const int & a_transparentRGB) {
	Uint32 colorkey = SDL_MapRGB( a_surface->format,
		0xff & (a_transparentRGB), 
		0xff & (a_transparentRGB >> 8), 
		0xff & (a_transparentRGB >> 16));
	SDL_SetColorKey( a_surface, SDL_SRCCOLORKEY, colorkey );
}

SDL_Surface * loadImage(string a_filename) {
	SDL_Surface * loadedImage = NULL;
	SDL_Surface * optimizedImage = NULL;
	loadedImage = IMG_Load(a_filename.c_str());

	if(loadedImage != NULL) {
		optimizedImage = SDL_DisplayFormat(loadedImage);
		SDL_FreeSurface(loadedImage);
	}

	return optimizedImage;
}

SDL_Surface * loadKeyImage(string a_filename) {
	SDL_Surface * loadedImage = NULL;
	SDL_Surface * optimizedImage = NULL;
	loadedImage = IMG_Load(a_filename.c_str());

	if(loadedImage != NULL) {
		optimizedImage = SDL_DisplayFormat(loadedImage);
		SDL_FreeSurface(loadedImage);

		if( optimizedImage != NULL ) {
			//Map the color key
			Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0, 0, 0 );
			SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
		} 
	}

	return optimizedImage; // return the optimized image
}